package com.study.servlet;

import com.github.pagehelper.PageHelper;
import com.study.entity.Student;
import com.study.mapper.StudentMapper;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/data")
public class ControllerServlet extends HttpServlet {
    private StudentMapper studentMapper;

    @Override
    public void init() throws ServletException {
        super.init();
        ServletContext servletContext = getServletContext();
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        studentMapper = webApplicationContext.getBean("studentMapperImpl", StudentMapper.class);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String type = request.getParameter("type");
        if ("studentList".equals(type)) {
            String pagecount = request.getParameter("pagecount");
            if(pagecount == null){
                pagecount = "1";
                request.setAttribute("pagecount",pagecount);
            }
            else{
                request.setAttribute("pagecount",pagecount);

            }
            PageHelper.startPage(Integer.parseInt(pagecount), 2);
            List<Student> studentList = studentMapper.getAllStudent();
            request.setAttribute("studentList", studentList);
            request.getRequestDispatcher("studentList.jsp").forward(request, response);
        } else if ("studentAdd".equals(type)) {
            String stuId = request.getParameter("stuId");
            String stuName = request.getParameter("stuName");
            String stuClass = request.getParameter("stuClass");
            String stuSpecial = request.getParameter("stuSpecial");
            String stuAge = request.getParameter("stuAge");
            String stuGender = request.getParameter("stuGender");
            Student stu = new Student();
            stu.setStuId(stuId);
            stu.setStuName(stuName);
            stu.setStuClass(stuClass);
            stu.setStuSpecial(stuSpecial);
            stu.setAge(Integer.parseInt(stuAge));
            stu.setGender(stuGender);

            int i = studentMapper.addStudent(stu);
            if (i == 1) {
                List<Student> allStudent = studentMapper.getAllStudent();
                request.setAttribute("studentList", allStudent);
                request.getRequestDispatcher("studentList.jsp").forward(request, response);
            } else {
                response.getWriter().write("网络错误，请您稍后再试");
            }
        } else if ("studentUpdate".equals(type)) {
            String stuId = request.getParameter("stuId");
            Student student = studentMapper.getStudentById(stuId);
            request.setAttribute("student", student);
            request.getRequestDispatcher("studentUpdate.jsp").forward(request, response);
        } else if ("studentUpdateSave".equals(type)) {
            String stuId = request.getParameter("stuId");
            String stuName = request.getParameter("stuName");
            String stuClass = request.getParameter("stuClass");
            String stuSpecial = request.getParameter("stuSpecial");
            int age = Integer.parseInt(request.getParameter("age"));
            String gender = request.getParameter("gender");
            Student student = new Student();
            student.setStuId(stuId);
            student.setStuName(stuName);
            student.setStuClass(stuClass);
            student.setStuSpecial(stuSpecial);
            student.setAge(age);
            student.setGender(gender);
            int i = studentMapper.updateStudent(student);
            if (i == 1) {
                List<Student> allStudent = studentMapper.getAllStudent();
                request.setAttribute("studentList", allStudent);
                request.getRequestDispatcher("studentList.jsp").forward(request, response);
            } else {
                response.getWriter().write("您的网络不通畅，请稍后再试");
            }
        } else if ("studentDelete".equals(type)) {
            String stuId = request.getParameter("stuId");
            int i = studentMapper.deleteStudentById(stuId);
            if (i == 1) {
                List<Student> allStudent = studentMapper.getAllStudent();
                request.setAttribute("studentList", allStudent);
                request.getRequestDispatcher("studentList.jsp").forward(request, response);
            } else {
                response.getWriter().write("您的网络不通畅，请稍后再试");
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

